package com.ruoyi.project.gzbook.authorinfo.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.gzbook.authorinfo.domain.DgAuthorInfo;
import com.ruoyi.project.gzbook.authorinfo.service.IDgAuthorInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 作者Controller
 * 
 * @author mhg
 * @date 2021-10-08
 */
@Controller
@RequestMapping("/gzbook/authorinfo")
public class DgAuthorInfoController extends BaseController
{
    private String prefix = "gzbook/authorinfo";

    @Value("${my_upload_path}")
    private String dirPath;

    @Autowired
    private IDgAuthorInfoService dgAuthorInfoService;

    @RequiresPermissions("gzbook:authorinfo:view")
    @GetMapping()
    public String authorinfo()
    {
        return prefix + "/authorinfo";
    }

    /**
     * 查询作者列表
     */
    @RequiresPermissions("gzbook:authorinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DgAuthorInfo dgAuthorInfo)
    {
        startPage();
        List<DgAuthorInfo> list = dgAuthorInfoService.selectDgAuthorInfoList(dgAuthorInfo);
        return getDataTable(list);
    }

    /**
     * 导出作者列表
     */
    @RequiresPermissions("gzbook:authorinfo:export")
    @Log(title = "作者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DgAuthorInfo dgAuthorInfo)
    {
        List<DgAuthorInfo> list = dgAuthorInfoService.selectDgAuthorInfoList(dgAuthorInfo);
        ExcelUtil<DgAuthorInfo> util = new ExcelUtil<DgAuthorInfo>(DgAuthorInfo.class);
        return util.exportExcel(list, "作者数据");
    }

    /**
     * 新增作者
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存作者
     */
    @RequiresPermissions("gzbook:authorinfo:add")
    @Log(title = "作者", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(String authorName, String coutry, MultipartFile file) throws IOException {
        DgAuthorInfo dgAuthorInfo = new DgAuthorInfo();
        dgAuthorInfo.setAuthorName(authorName);
        dgAuthorInfo.setCoutry(coutry);

        //保存文件到相关路径
        //得到原始文件名
        String fileName = file.getOriginalFilename();
        //得到后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一文件名
        fileName = UUID.randomUUID()+suffix;
        //保存图片的路径

        File dirFile = new File(dirPath);
        //判断如果文件夹不存在 创建
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirPath+"/"+fileName;
        //保存文件  异常抛出
        file.transferTo(new File(filePath));
        //保存文件名
        dgAuthorInfo.setImagePath(fileName);
        return toAjax(dgAuthorInfoService.insertDgAuthorInfo(dgAuthorInfo));
    }

    /**
     * 修改作者
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DgAuthorInfo dgAuthorInfo = dgAuthorInfoService.selectDgAuthorInfoById(id);
        mmap.put("dgAuthorInfo", dgAuthorInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存作者
     */
    @RequiresPermissions("gzbook:authorinfo:edit")
    @Log(title = "作者", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DgAuthorInfo dgAuthorInfo)
    {
        return toAjax(dgAuthorInfoService.updateDgAuthorInfo(dgAuthorInfo));
    }

    /**
     * 删除作者
     */
    @RequiresPermissions("gzbook:authorinfo:remove")
    @Log(title = "作者", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dgAuthorInfoService.deleteDgAuthorInfoByIds(ids));
    }


    /**
     * 加载所有菜单列表树
     */
    @RequiresPermissions("gzbook:authorinfo:list")
    @GetMapping("/treeAuthor")
    @ResponseBody
    public List<Ztree> authorData()
    {
        DgAuthorInfo query = new DgAuthorInfo();
        List<DgAuthorInfo> list = dgAuthorInfoService.selectDgAuthorInfoList(query);
        List<Ztree> ztrees = new ArrayList<>();
        for (DgAuthorInfo pojo : list){
            Ztree ztree = new Ztree();
            ztree.setId(pojo.getId());
            ztree.setName(pojo.getAuthorName()+ "&nbsp;&nbsp;&nbsp;" +pojo.getCoutry());
            ztree.setTitle(pojo.getAuthorName());
            ztree.setpId(null);
            ztree.setChecked(false);
            ztree.setNocheck(false);
            ztree.setOpen(false);
            ztrees.add(ztree);
        }
        logger.info("作者集合为--：" + ztrees);
        return ztrees;
    }

    /**
     * 选择作者
     */
    @RequiresPermissions("gzbook:authorinfo:view")
    @GetMapping("/selectAuthor/{authorId}")
    public String selectMenuTree( @PathVariable("authorId") Long authorId, ModelMap mmap)
    {
        logger.info("传过来的参数为authorId--："+ authorId);
        DgAuthorInfo pojo = dgAuthorInfoService.selectDgAuthorInfoById(authorId);
        logger.info("查询结果--："+ pojo);
        mmap.put("author", pojo);
        return prefix + "/tree";
    }


}
